import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {UserService} from "../../../providers/user-service";
import {AppPopup} from "../../../util/popup";

export class UserInfo {
    phone: string;
    yzm: string;
    newPassWord: string;
    rePassWord: string;
}

@Component({
    selector: 'find-pwd',
    templateUrl: 'find-pwd.html'
})
export class FindPwd {
    hasClick = false;
    errorTip = false;        //为true显示错误提示
    errorTipMsg = "";
    hq = "获取验证码";
    num = 120;
    showAlert = true;       //修改成功的提示

    constructor(public navCtrl: NavController,
                private userServ: UserService,
                private popup: AppPopup,
                ) {}

    userInfo: UserInfo = {
        phone: '',
        yzm: '',
        newPassWord: '',
        rePassWord: ''
    };


    //找回密码--获取验证码
    findPasswordCode() {
        if(!this.userInfo.phone) {
            this.errorTip = true;
            this.errorTipMsg = "请输入手机号";
        }else if (!(/^1[34578]\d{9}$/.test(this.userInfo.phone))) {
            this.errorTip = true;
            this.errorTipMsg = "请输入正确的手机号";
        } else {
            this.userServ.findPasswordCode({"mobile": this.userInfo.phone})
                .then(data => {
                    console.info("findPasswordCode,获取验证码成功：",data);
                    this.errorTip = false;
                    this.hasClick = true;
                    let timer = setInterval(() => {
                        if (this.num == 0) {
                            clearInterval(timer);
                            this.hasClick = false;
                            this.hq = "重新获取";
                            this.num = 120;
                            return;
                        }
                        this.num--;
                    }, 1000);
                })
                .catch(err => {
                    console.error('异常信息', err);
                    this.errorTip = true;
                    this.errorTipMsg = err.errorMassage;
                });
        }
    }




    //找回密码--修改密码
    findPassword() {
        console.log(this.userInfo.newPassWord)
        // var pwdTest = /^(((?=.*[0-9])(?=.*[a-zA-Z])|(?=.*[0-9])(?=.*[^\s0-9a-zA-Z])|(?=.*[a-zA-Z])(?=.*[^\s0-9a-zA-Z]))[^\s]+)$/;
        var pwdTest = /^(?![\d]+$)(?![a-zA-Z]+$)(?![-!@#$%^&*()_+.{}`=|\/\[\]\\\'?;\':,<>]+$)[\da-zA-Z-!@#$%^&*()_+.{}`=|\/\[\]\\\'?;\':,<>]{6,32}$/;
        if(!this.userInfo.phone){

            this.errorTip = true;
            this.errorTipMsg = "请输入手机号";
        } else if (!(/^1[34578]\d{9}$/.test(this.userInfo.phone))) {
            this.errorTip = true;
            this.errorTipMsg = "请输入正确的手机号！";
        } else if (this.userInfo.yzm == null || this.userInfo.yzm == "") {
            this.errorTip = true;
            this.errorTipMsg = "请输入验证码！";
        } else if (this.userInfo.newPassWord.length < 6 || this.userInfo.newPassWord.length > 32) {
            this.errorTip = true;
            this.errorTipMsg = "密码长度必须在6-32位之间！";
        } else if (!pwdTest.test(this.userInfo.newPassWord) || !pwdTest.test(this.userInfo.rePassWord)) {
            this.errorTip = true;
            this.errorTipMsg = "密码只能由字母、数字、特殊字符的两种或三种组合成！";
        } else if (this.userInfo.newPassWord != this.userInfo.rePassWord) {
            this.errorTip = true;
            this.errorTipMsg = "两次输入的密码不一致！";
        } else {
            this.errorTip = false;
            //调用接口
            console.log('调用找回密码接口');
            let param = {
                mobile: this.userInfo.phone,
                password: this.userInfo.newPassWord,
                verificationCode: this.userInfo.yzm
            };
            this.userServ.findPassword(param)
                .then(data => {
                    console.info("findPassword，密码修改成功",data);
                    this.showAlert = false;
                })
                .catch(err => {
                    console.error('findPassword，密码修改失败', err);
                    this.errorTip = true;
                    this.errorTipMsg = err.errorMassage;
                });
        }
    }


    btnChoose() {
        this.showAlert = true;
        this.navCtrl.popToRoot();
    }

    //返回
    goBack() {
        this.navCtrl.popToRoot();
    }
}

